var g_sReportSystemPath ="/bbxt";

/**
 *参考输入组织机构中的人
 *
 *@sScope 参考输入范围 "local","global";
 *@sMulti 是否多选 "true","false";
 *@sTitle reference title;
*/
function refPerson(sScope, sMulti, sTitle){
	var s = new Object();
	s.refResource = "org.child";
	s.refDisplayType = "0123";
	s.refAllowType = "3";
	//s.refKey = "secure=true";
	if("true" == sMulti) {
		s.refMultiple = "true";
	}else {
		s.refMultiple = "false";
	}
	if("local" == sScope) {
		var code = queryCompanyCode();
		var companyId = queryCompanyId(code);
		s.refRootKey = companyId;
	}
	s.refDialog = "tree";
	if(null == sTitle) {
		sTitle = "请选择相关人";
	}

	var res = HWNode.reference(s, null, sTitle);
	return res;
}

/* 查询服务器所在单位在Codes表中的代码 */
function queryCompanyCode(){
	var companyCode = queryLocalSysParam("site%1");
	return companyCode;
}

function queryLocalSysParam(paramName){
	var __g_soap_ServicePath = "/hwsvr2/se/admin";
	var sXML = "<OType>svr</OType>";
	sXML += "<OURL>local</OURL>";
	sXML += "<PName>" + paramName + "</PName>";
	var soap;
	soap = new HWSOAP();
	soap.addRequest("get_SysParam",null,sXML);
	soap.send(__g_soap_ServicePath);
	var sr = soap.getResult();
	if(sr==null) {
		soap.showErrors();
		return null;
	}
	var SysParam = sr.selectSingleNode("SysParams/SysParam");
	if(null == SysParam){
		alert("您还未配置/etc/pa/params.xml中svr.company！");
		return null;
	}

	var oPValue = SysParam.selectSingleNode("PValue");
	if(null == oPValue){
		alert("您还未配置/etc/pa/params.xml中svr.company！");
		return null;
	}
	
	var paramValue = oPValue.text;	
	return paramValue;
}

/* 查询服务器所在单位的在目录服务器中代码 */
function queryCompanyId(code){
	if(null == code){
		alert("code不能为空!");
		return null;
	}
	var newCode = parseInt(code).toString(16);
	var soap = new HWSOAP();
	var sConfig = "Components.ReportManager.ReportDefinition.DataConfig.conf_dscontainers";
	soap.addRequest("queryData", "<c>"+sConfig+"</c><pv>0x101</pv>", "<ps><pv nm=\"code\">" + newCode + "</pv></ps>");
	soap.send("/hwsvr2/qe");
	var r = soap.getResult(0);
	if(r==null) {
		soap.showErrors();
		return null;
	}
	r = r.selectSingleNode("hwdata/dsc");
	if(null == r){
		return null;
	}

	var companyId = r.selectSingleNode("id");
	if(null == companyId){
		return null;
	}
	return companyId.text;
}

function refSerialKey(multi){
	var soap = new HWSOAP("/hwsvr2/re/admin");
	var sData =  "<uri>" + g_uri + "</uri>";
	soap.addRequest("getSerial",null,sData);
	soap.send();
	var sr = soap.getResult();
	if (null==sr)  { soap.showErrors(); return;}
	
	var sXML = "<options>";
	sXML += "<tag>";
	sXML += "<id>value</id>";
	sXML += "<display>title</display>";
	sXML += "<help>name</help>";
	sXML += "<objects>codes</objects>";
	sXML += "</tag>";
	sXML += createCodes(sr);
	sXML += "</options>";
	
	var XMLDoc = new ActiveXObject("Microsoft.XMLDOM");
	XMLDoc.async = false;	
	XMLDoc.loadXML(sXML);
	var xmlNode = XMLDoc.selectSingleNode("options");	

	var s = new Object();
	if(null == multi){
		s.refMultiple = "false";	
	}else{
		s.refMultiple = "true";	
	}
	var sTitle = "请选择报表序列值";
	var result = WinManager.dialog("/bbxt/includes/ref/refXMLTree.html",new Array(xmlNode,s,null,null,sTitle));	
	return result;
}

function setParam(sParams,sParamName,sParamValue){
	sParams += "<pm nm='" + sParamName + "'>" + sParamValue + "</pm>";
	return sParams;	
}


/**
	根据BBID和URI查看报表
*/
function editReport(iBbid, sUri){
	window.open('/application.html?g_sPath="/Application/ReportManager/ViewReport"&uri="'+sUri+'"&bbid="'+iBbid+'"&g_readonly="false"');
}

/**
	利用中间件创建报表
*/
function adminRe(sAction, sParams, sDataXML) {
	var soap = new HWSOAP();
	soap.addRequest(sAction, sParams, sDataXML);
	soap.send("/hwsvr2/re/admin");
	var sr = soap.getResult();
	if (null == sr) {
		soap.showErrors();
	}
	return sr;
}

/**
 * 检查需要提交的数据列表（多条数据）
 * @param oChildList 需要验证的XML节点列表
 * @param aXPath 验证XML节点的XPath数组
 * @aWarn 验证不通过的警告信息
 * @aConfirm 验证时不通过的提示信息
 */
function checkRecords(oChildList, aXPath, aWarn, aConfirm) {		
	var aRecord = new Array();
	for (var i = 0; (typeof aXPath != "undefined") && (i < aXPath.length); i++) {
		aRecord[i] = new Array();	
	}
	
	sXML = "";
	for (var i = 0; i < oChildList.length; i++) {
		var bError = false;
		for (var j = 0; (typeof aXPath != "undefined") && (j < aXPath.length); j++) {
			var len = oChildList[i].selectNodes(aXPath[j]).length;
			if(len > 0) {
				if (typeof aWarn != "undefined" && aWarn[j] != "") {
					aRecord[j][aRecord[j].length] = i + 1;	
					bError = true;
				}
				
				if (typeof aConfirm != "undefined" && aConfirm[j] != "") {
					if(!confirm(aConfirm[j].replace("@@",i+1))) {
						bError = true;
					}	
				}
			}		
		}
		
		if (bError == true) {
			continue;	
		}
		
		var bbid = xml_text(oChildList[i].selectSingleNode("bbid"));
		var uri = xml_text(oChildList[i].selectSingleNode("uri"));
		sXML += "<rp uri=\""+ uri +"\">" + bbid + "</rp>";
	}	
	
	var sErrorMsg = ""; 
	for (var i = 0; (typeof aXPath != "undefined") && (i < aXPath.length); i++) {
		if ((typeof aWarn != "undefined") && (aRecord[i].length > 0)) {
			sErrorMsg += aWarn[i].replace("@@",aRecord[i].join(",")) + "\r\n";	
		}
	}
	
	var result = new Object();
	result.fail = sErrorMsg;
	result.succss = sXML;
	return result;
}

/**
 * 检查需要提交的数据列（单个数据）
 * @param oChild 需要验证的XML节点列表
 * @param aXPath 验证XML节点的XPath数组
 * @aWarn 验证不通过的警告信息
 * @aConfirm 验证时不通过的提示信息
 */
function checkRecord(oChild, aXPath, aWarn, aConfirm) {		
	var aRecord = new Array();
	for (var i = 0; (typeof aXPath != "undefined") && (i < aXPath.length); i++) {
		aRecord[i] = new Array();	
	}
	
	sXML = "";
		var bError = false;
		for (var j = 0; (typeof aXPath != "undefined") && (j < aXPath.length); j++) {
			var len = oChild.selectNodes(aXPath[j]).length;
			if(len > 0) {
				if (typeof aWarn != "undefined" && aWarn[j] != "") {
					aRecord[j][aRecord[j].length] = i + 1;	
					bError = true;
				}
				
				if (typeof aConfirm != "undefined" && aConfirm[j] != "") {
					if(!confirm(aConfirm[j].replace("@@",i+1))) {
						bError = true;
					}	
				}
			}		
		}
		
		var bbid = xml_text(oChild.selectSingleNode("bbid"));
		var uri = xml_text(oChild.selectSingleNode("uri"));
		sXML += "<rp uri=\""+ uri +"\">" + bbid + "</rp>";
	
	var sErrorMsg = ""; 
	for (var i = 0; (typeof aXPath != "undefined") && (i < aXPath.length); i++) {
		if ((typeof aWarn != "undefined") && (aRecord[i].length > 0)) {
			sErrorMsg += aWarn[i].replace("@@",aRecord[i].join(",")) + "\r\n";	
		}
	}
	
	var result = new Object();
	result.fail = sErrorMsg;
	result.succss = sXML;
	
	return result;
}